<template>
    <div id="admin-carousel">
      <!-- 管理轮播图按钮 -->
      <el-button type="primary" @click="toggleAdminMode">
        {{ adminMode ? "退出管理" : "进入管理" }}
      </el-button>
  
      <!-- 轮播图组件 -->
      <div id="main" v-if="!adminMode">
        <el-carousel height="20vh" :autoplay="true" type="card">
          <el-carousel-item v-for="item in imgs" :key="item.id">
            <img :src="item.url" :alt="item.id">
          </el-carousel-item>
        </el-carousel>
        <p>见证每一刻的光点</p>
      </div>
  
      <!-- 管理轮播图的表单 -->
      <div v-else>
        <el-card>
          <h2>管理轮播图</h2>
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="图片ID">
              <el-input v-model="form.id" placeholder="请输入图片ID"></el-input>
            </el-form-item>
            <el-form-item label="上传图片">
              <input type="file" @change="handleFileUpload" accept="image/*" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addImg">添加图片</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="imgs" style="width: 100%">
            <el-table-column prop="id" label="ID" width="180"></el-table-column>
            <!-- <el-table-column prop="url" label="图片URL" width="700"></el-table-column> -->
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button type="text" @click="deleteImg(scope.row)">删除</el-button> 
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'AdminCarousel',
    data() {
      return {
        adminMode: false, // 是否处于管理模式
        imgs: [], // 轮播图图片数组
        form: {
          url: '', // 新增图片的URL（Base64格式）
          id: '' // 新增图片的ID
        }
      };
    },
    mounted() {
      this.imgs = JSON.parse(localStorage.getItem('imgs')) || [];
    },
    methods: {
      // 切换管理模式
      toggleAdminMode() {
        this.adminMode = !this.adminMode;
      },
      // 处理文件上传
      handleFileUpload(event) {
        const file = event.target.files[0];
        if (file) {
          const reader = new FileReader();
          reader.onload = (e) => {
            this.form.url = e.target.result; // 将图片转换为Base64格式
          };
          reader.readAsDataURL(file);
        }
      },
      // 添加图片
      addImg() {
        if (this.form.url && this.form.id) {
          const newImg = {
            url: this.form.url,
            id: this.form.id
          };
          this.imgs.push(newImg);
          this.saveImgs();
          this.form.url = '';
          this.form.id = '';
          this.$message.success('图片添加成功');
        } else {
          this.$message.error('请上传图片并填写ID');
        }
      },
      // 删除图片
      deleteImg(row) {
        const index = this.imgs.findIndex(img => img.id === row.id);
        if (index !== -1) {
          this.imgs.splice(index, 1);
          this.saveImgs();
          this.$message.success('图片删除成功');
        }
      },
      // 编辑图片
      editImg(row) {
        this.form.url = row.url;
        this.form.id = row.id;
        this.$nextTick(() => {
          this.$refs.form.validate(valid => {
            if (valid) {
              const index = this.imgs.findIndex(img => img.id === row.id);
              if (index !== -1) {
                this.imgs[index] = {
                  url: this.form.url,
                  id: this.form.id
                };
                this.saveImgs();
                this.$message.success('图片编辑成功');
              }
            }
          });
        });
      },
      // 保存图片信息到localStorage
      saveImgs() {
        localStorage.setItem('imgs', JSON.stringify(this.imgs));
      }
    }
  };
  </script>
  
  <style scoped>
  #main {
    width: 100%;
    padding: 2vw;
    background-color: #f8fafc;
  }
  
  p {
    font-size: 48px;
    font-weight: bold;
    background: linear-gradient(to right, red, blue);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }
  
  img {
    width: 100%;
  }
  
  #admin-carousel {
    padding: 20px;
  }
  
  input[type="file"] {
    display: block;
    margin-bottom: 10px;
  }
  </style>